﻿using aier.dataaccess.model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    [RoutePrefix("api/User")]
    public class UserController : ApiController
    {
        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }

        public ResResult resResult { get; set; }

        /// <summary>
        /// 取得当前登录人基本信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IHttpActionResult Get()
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            return Ok<UserInfo>(userInfo);
        }

        /// <summary>
        /// 退出登录时移除缓存
        /// </summary>
        /// <param name="pwd">区分GET请求</param>
        /// <returns></returns>
        [Authorize]
        [ActionName("LogOff")]
        [HttpGet]
        public IHttpActionResult LogOff(string pwd)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            ObjectCache oCache = MemoryCache.Default;
            var cacheKeies = oCache.Where(dic => dic.Key.StartsWith(userInfo.domainname?.ToLower()))?.Select(dic => dic.Key);
            cacheKeies.ToList().ForEach(key =>
            {
                oCache.Remove(key);
            });
            return Ok(resResult.SuccessResult(1));
        }
    }
}
